package com.tmall.tmall.controller.admin;

import com.tmall.tmall.pojo.Advertising;
import com.tmall.tmall.pojo.PageRt;
import com.tmall.tmall.pojo.Result;
import com.tmall.tmall.service.AdvertisingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/advertising")
public class AdminAdvertisingController {
    @Autowired
    private AdvertisingService advertisingService;

    /**
     * 增
     * @param advertising
     * @return
     */

    @PostMapping("/add")
    public Result add(@RequestBody Advertising advertising){
        //查询有无位置重复
        Advertising gt=advertisingService.getByPosition(advertising.getPosition());
        //位置被占了
        if(gt!=null){
            return Result.error("位置被占,换个位置");
        }
        //插入
        advertisingService.insert(advertising);
        return Result.success();
    }

    /**
     * 查
     * @param pageNum
     * @param pageSize
     * @param position
     * @return
     */
    @GetMapping("/page")
    public Result Page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       Integer position){
        PageRt list=advertisingService.page(pageNum,pageSize,position);
        return Result.success(list);
    }

    /**
     * 改
     * @param advertising
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Advertising advertising){
        //查询有无位置重复
        Advertising gt=advertisingService.getByPosition(advertising.getPosition());
        //位置被占了
        if(gt!=null && gt.getId()!=advertising.getId()){
            return Result.error("位置被占,换个位置");
        }
        advertisingService.update(advertising);
        return Result.success();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delBatch")
    public Result delete(@RequestBody List<Integer> ids){
        advertisingService.delete(ids);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(){
        List<Advertising> list = advertisingService.selectAll();
        return Result.success(list);
    }
}
